package com.younion.action.manage;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.younion.common.base.BaseAction;
import com.younion.common.paginator.Paginator;

import cpcns.jhsks.util.MapCollectionUtils;

import com.younion.business.ICardAgioService;
import com.younion.vo.CardAgioVO;
import com.younion.vo.CustomerVO;

/**
 * @author wells
 */
public class CardAgioAction extends BaseAction {

    private static final long serialVersionUID = 1L;

    protected static final Log logger = LogFactory.getLog(CardAgioAction.class);

    private ICardAgioService cardAgioService = null;

    private CardAgioVO cardAgioVO = new CardAgioVO();

    private List<CardAgioVO> cardAgioList;// 结果集列表

    /**
     * 列表：翻页区；数据区
     */
    @SuppressWarnings("unchecked")
    public String listCardAgio() {
        try {
            Map paramMap = null;
            if (getSessionUser().getCustomer() != null) {
                cardAgioVO.setCustomerVO(getSessionUser().getCustomer());
            } else if (cardAgioVO != null && cardAgioVO.getCustomerId() != null) {
                cardAgioVO.setCustomerVO(new CustomerVO(cardAgioVO.getCustomerId()));
            }
            if (cardAgioVO != null) {
                paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(cardAgioVO));
            }
            // 获取总记录数
            int total = cardAgioService.getCountByMap(paramMap);
            // 根据总记录数获取分页对象
            Paginator paginator = this.getPaginator(total);
            cardAgioList = cardAgioService.get(paramMap, paginator);
            // 保存分页对象
            this.savaPaginator(paginator);
            this.addLog(2, "查询会员卡折扣列表", "查询会员卡折扣列表", this.getSessionUser());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return SUCCESS;
    }

    /**
     * 新增
     */
    public String addCardAgio() {
        return SUCCESS;
    }

    /**
     * 修改保存，调度Jsp
     */
    public String editCardAgio() {
        try {
            cardAgioVO = cardAgioService.getById(cardAgioVO.getId().toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    /**
     * 保存动作
     */
    public String saveCardAgio() {
        try {
            cardAgioVO = cardAgioService.save(cardAgioVO);
            Integer customerId = getSessionUser().getCustomer() == null ? -1 : getSessionUser().getCustomer().getId();
            memoryCacheManager.invalidateCardAgio(customerId);
            String id = cardAgioVO.getId() == null ? "" : cardAgioVO.getId().toString();
            String log = "".equals(id) ? "添加会员卡折扣" : "更新会员卡折扣";
            int type = "".equals(id) ? 3 : 4;
            this.addLog(type, log + id, log, getSessionUser());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        }
        request.setAttribute("refreshNavTabId", "cardAgioList");
        return SUCCESS;
    }

    /**
     * 删除
     */
    public String deleteCardAgio() {
        try {
            String pkId = request.getParameter("key");
            cardAgioService.remove(Arrays.asList(pkId.split(",")));
            Integer customerId = getSessionUser().getCustomer() == null ? -1 : getSessionUser().getCustomer().getId();
            memoryCacheManager.invalidateCardAgio(customerId);
            String log = "删除会员卡折扣";
            this.addLog(5, log + pkId, log, getSessionUser());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        }
        request.setAttribute("refreshNavTabId", "cardAgioList");
        return SUCCESS;
    }

    /**
     * 查看鿴
     */
    public String viewCardAgio() {
        try {
            String pkId = request.getParameter("key"); 
            cardAgioVO = cardAgioService.getById(pkId);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            e.printStackTrace();
        }
        return SUCCESS;

    }

    public CardAgioVO getCardAgioVO() {
        return cardAgioVO;
    }

    public void setCardAgioVO(CardAgioVO cardAgioVO) {
        this.cardAgioVO = cardAgioVO;
    }

    public void setCardAgioService(ICardAgioService cardAgioService) {
        this.cardAgioService = cardAgioService;
    }

    public List<CardAgioVO> getCardAgioList() {
        return cardAgioList;
    }

    public void setCardAgioList(List<CardAgioVO> cardAgioList) {
        this.cardAgioList = cardAgioList;
    }

}